-- UIGainItemPath
-- Created by huanghr Jul/07/2015
-- 产出途径界面

require "game/ui/form/main/UISubAreaList"
require "game/ui/form/item/UIMaterialCompound"
require "game/ui/form/market/UIMarketMain"
require "game/ui/form/item/UIBuyFood"
require "game/ui/form/market/UIScrapMarket"
require "game/ui/form/shop/UIScrapShop"
require "game/ui/form/sky_city/UISkyShop"

-- UIGainItemPath继承自Layer
UIGainItemPath = class("UIGainItemPath", function()
    return cc.Layer:create();
end);

function UIGainItemPath.create(classId, tabs, openType)
    return UIGainItemPath.new(classId, tabs, openType);
end

-- 界面打开类型
OPEN_TYPE_AIR_SHIP = "air_ship";
OPEN_TYPE_NORMAL   = "normal";

local LIFE_GOODS_ID = 2003;
local ARTIFACT_WORKSHOP_ID = 16;

-- 产出途径
local DUNGEON_AREA        = 1;          -- 地图掉落
local DUNGEON_BOSS        = 2;          -- 地图BOSS掉落
local WORKSHOP_PRODUCTION = 3;          -- 炼金工坊产出
local BLACKMARKET_SELL    = 4;          -- 旅行商人出售
local RISKER_BONUS        = 5;          -- 冒险系称号
local GAME_ACHIV          = 6;          -- 成就奖励
local SIGN_IN_BONUS       = 8;          -- 每日签到奖励
local MARKET_SELL         = 11;         -- 商城出售
local MARKET_BUY          = 14;         -- 黑市购买
local GOLD_POT            = 16;         -- 黄金罐的抽取
local DUNGEON_SECRET      = 17;         -- 地图隐秘事件
local SCRAP_SHOP          = 18;         -- 佣兵营地碎片兑换
local BUY_LIFE_EVENT      = 20;         -- 购买活力触发事件
local BLACKMARKET_EVENT   = 21;         -- 旅行商人触发事件
local BUY_GOLD_POT_EVENT  = 23;         -- 购买黄金罐触发事件
local WORKSHOP_SUMMON     = 28;         -- 炼金坊的召唤
local ARTIFACT_EVENT      = 29;         -- 神器工坊触发事件
local SKY_SHOP            = 33;         -- 天空集市
local GOLD_POT2           = 38;         -- 黄金罐的抽取(非活动期间)
local MARKET_SELL2        = 39;         -- 商城出售(活动期间)

-- 构造函数
function UIGainItemPath:ctor(classId, tabs, openType)
    -- 初始化
    self:setName("UIGainItemPath");
    local node = cc.CSLoader:createNode("layout/item/GainItemPath.csb");
    self:addChild(node);
    self.node = node;

    self.classId = classId;

    self.openType = openType == "air_ships" and OPEN_TYPE_AIR_SHIP or OPEN_TYPE_NORMAL;

    -- 当前标签页与上一层相同
    if not tabs then
        tabs = {};
    end
    self.tab = tabs;
    table.insert(self.tab, {1, classId});

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    self:initData();

    -- 适配
    self:resize();
end

-- 适配
function UIGainItemPath:resize()

    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIGainItemPath:redraw()
    -- 绘制公共模板背景
    self:redrawTemplate();

    -- 重绘物品及描述
    self:redrawItemAndDesc();

    -- 重绘标签页
    self:redrawTabs();
end

-- 绘制标签页
function UIGainItemPath:redrawTabs()
    local tabNode = findChildByName(self.node, "CT/tabs");
    local tabNum = #self.tab;

    -- 先移除旧的标签
    tabNode.rows = {};
    tabNode:removeAllChildren();

    -- 根据tab数据，生成新标签
    local widget = replaceNodeWithWidget(cc.CSLoader:createNode("layout/tab/tab.csb"));

    for i = 1, tabNum do
        local child = widget:clone();
        child:setName("tab" .. i);
        child.index = i;

        self:registerTabTouchEvent(child);

        -- 计算单元格坐标
        -- 这个坐标将是单元格的描点所在
        local x = (i - 1) * 68 - 210;
        local y = -34;

        child:setPositionX(x);
        child:setPositionY(y);

        local tabLight = findChildByName(child, "light");
        local tabImage = findChildByName(child, "tab");
        if i == tabNum then
            tabImage:loadTexture("images/ui/tab/red.png");
        end
        tabLight:setScaleX(0.7);
        tabImage:setScaleX(0.7);

        if i == tabNum then
            tabLight:setVisible(true);
        else
            tabLight:setVisible(false);
        end

        local tabText = findChildByName(child, "text");
        local tabIcon = findChildByName(child, "icon");

        tabText:setVisible(false);
        tabIcon:loadTexture(getItemIconPath(ItemM.query(self.tab[i][2], "icon")));
        tabIcon:setScale(0.5);

        tabNode:addChild(child)
        table.insert(tabNode.rows, child)
    end
end

-- 绘制公共模板背景
function UIGainItemPath:redrawTemplate()
    -- 更换公共模板上方2侧的图片
    local bgType1 = findChildByName(self.node, "CT/template/bg_type");
    local bgType2 = findChildByName(self.node, "CT/template/bg_type2");

    -- 根据要合成的物品类型，选择界面的类型
    local itemGroup = ItemM.query(self.classId, "group");
    if itemGroup == 19 or itemGroup == 20 then
        -- 是食物或者食材
        bgType1:loadTexture(getTitleBgIconPath("food"));
        bgType2:loadTexture(getTitleBgIconPath("food"));
    else
        bgType1:loadTexture(getTitleBgIconPath("upgrade"));
        bgType2:loadTexture(getTitleBgIconPath("upgrade"));
    end

    -- 绘制标题
    local titleLabel = findChildByName(self.node, "CT/template/title");
    local subHeadLabel = findChildByName(self.node, "CT/template/subhead");

    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(ItemM.query(self.classId, "name"));

    -- 当前拥有的数量
    local itemAmount = ItemM.getAmount(ME.user, self.classId);
    TextStyleM.setSubheadStyle(subHeadLabel);
    --  拥有%d件
    subHeadLabel:setString(string.format(getLocStr("own_num"), itemAmount));
    TextStyleM.setSubheadStyle(subHeadLabel);
end

-- 重绘食物信息
function UIGainItemPath:redrawItemAndDesc()
    local item = findChildByName(self.node, "CT/item");

    -- 物品图标
    local iconImg = findChildByName(item, "icon");
    iconImg:loadTexture(getItemIconPath(ItemM.query(self.classId, "icon")));

    -- 物品星级
    local starNode = findChildByName(item, "star_node");
    local rank = ItemM.query(self.classId, "rank");
    local layer = createStarLayer(rank, "images/ui/symbol/star_little.png", "center", -0.7);
    if layer then
        starNode:removeAllChildren();
        starNode:addChild(layer);
    end

    -- 物品的背景框
    local frameImage = findChildByName(item, "bg");
    frameImage:loadTexture(getFrameBgIconPath("rank", 1));
end

-- 注册标签页点击回调
function UIGainItemPath:registerTabTouchEvent(node)
    local tabImage = node:getChildByName("tab");
    local function onClick(sender, eventType)
        -- 点击时的回调
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            if node.index < #self.tab then
                local uiForm = UIMgr.getCurrentScene():getFormByName("UIItemCompound");
                if uiForm then
                    uiForm:itemPathCallback(node.index);
                end
                UIMgr.getCurrentScene():removeFormByName("UIGainItemPath");
            end
        end
    end

    tabImage:addTouchEventListener(onClick);
end

-- 注册点击事件
function UIGainItemPath:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            local index = #self.tab - 1;
            index = math.max(index, 1);
            local uiForm = UIMgr.getCurrentScene():getFormByName("UIItemCompound");
            if uiForm then
                uiForm:itemPathCallback(index);
                uiForm:setTopMenueNoChange(true);
            end
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIGainItemPath");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end


function UIGainItemPath:registerEventCallback()
    -- 注册获得焦点的回调
    EventMgr.register("UIGainItemPath", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UIGainItemPath" then
            if self.openType == OPEN_TYPE_AIR_SHIP then
                UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_RUINS_SCRAP);
            else
                UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_GEM);
            end
        end
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIGainItemPath界面析构清理");
            EventMgr.removeAll("UIGainItemPath");
        elseif eventType == "enter" then

        end
    end);
end

-- 记录合成序列
function UIGainItemPath:tryToCompound(ruleId)
    if not CompoundM.doCompound(ruleId, "compound") then
        return;
    end

    if not self.compoundList then
        self.compoundList = {};
    end

    local compoundNum = 0;
    if #self.compoundList > 0 then
        if self.compoundList[1] ~= ruleId then
            self:clearCompoundList();
        else
            compoundNum = self.compoundList[2];
        end
    end

    self.compoundList = {ruleId, compoundNum + 1};
end

-- 初始化奖励列表
function UIGainItemPath:initData()
    local scrollview = findChildByName(self.node, "CT/scroll_view");
    scrollview:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/item/PathInfo.csb");
    local widget = replaceNodeWithWidget(itemNode);

    -- 获取途径列表
    local pathList = GainItemPathM.query(self.classId, "paths");

    if pathList == nil then
        return;
    end

    -- 左边距
    local leftMargin = 1;

    -- 行间距
    local gap = 0;
    local widgetHeight = 61;

    local totalNum = 0;
    for k,v in pairs(pathList) do
        totalNum = totalNum + 1;
    end

    local startHeight = 10;

    --计算总高度
    local totalHeight = math.max(totalNum * (widgetHeight + gap), scrollview:getInnerContainerSize().height);
    startHeight = totalHeight;

    if totalHeight < scrollview:getInnerContainerSize().height then
        startHeight = scrollview:getInnerContainerSize().height;
    end

    scrollview:setInnerContainerSize(cc.size(scrollview:getInnerContainerSize().width, totalHeight));

    for n = 1, #pathList do
        local info = pathList[n];
        local child = widget:clone();
        local x = leftMargin;
        local y = startHeight - n * (widgetHeight + gap) - 10;
        if n == 1 then
            local line2 = findChildByName(child, "line2");
            line2:setVisible(true);
        end
        child:setPositionX(x);
        child:setPositionY(y);

        -- 初始化奖励
        child:setTag(n);
        self:initItem(child, info);

        scrollview:addChild(child);
    end
end

-- 初始化奖励项
function UIGainItemPath:initItem(grid, info)
    local nameLabel = findChildByName(grid, "path_label");

    local wayType = info;
    if type(info) == "table" then
        wayType = info[1];
    end

    local wayName = GainItemWaysM.query(wayType, "name");
    local goImg = findChildByName(grid, "go");
    local touchImg = findChildByName(grid, "touch_bg");

    local kerning = getLocKerning() - 3;
    if not isFullWidthLang() then
        kerning = getLocKerning() - 1;
    end
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE, false, kerning);

    local isLink = GainItemWaysM.query(wayType, "is_link");

    if wayType == MARKET_SELL2 then
        -- 活动期间商城购买
        isLink = iif(ActivityDungeonM.getCurrentActivityId() == info[2], 1, 0);
    end

    -- 根据实际能否链接来判断文字说明
    local buyImg = findChildByName(grid, "buy");

    -- 不做链接的途径置灰，不可用
    if isLink == 0 then
        goImg:setVisible(false);
        touchImg:setVisible(false);
        nameLabel:setString(wayName);
        TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
        if wayType == RISKER_BONUS then
            local careerName = CareerM.getCareerInfo(info[2], "name");
            local str = getLocStr("career_bonus_tip");
            nameLabel:setString(wayName .. string.format(str, careerName));
        end

    elseif isLink == 1 then
        -- 不同路径的选择

        if wayType == DUNGEON_AREA then
            -- 判断地图是否开启
            local areaName = DungeonAreaM.query(info[2], "name");
            nameLabel:setString(spliceLocStr(areaName, wayName));

            if DungeonAreaM.isParentAreaUnlocked(ME.user, info[2]) then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
                nameLabel:setString(spliceLocStr(areaName, wayName, getLocStr("not_open")));
            end

            -- 注册链接到关卡
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    AudioM.playFx("button_click");
                    if UIMgr:getCurrentScene():isOpen("UISubAreaList") then
                        UIMgr:getCurrentScene():removeForm("UISubAreaList");
                    end

                    local uiItemPath = UISubAreaList.create(info[2]);
                    UIMgr.getCurrentScene():addForm(uiItemPath);
                end
            end
            goImg:addTouchEventListener(onGoClick);
            -- 注册点击事件
            touchImg:addTouchEventListener(onGoClick);

        elseif wayType == DUNGEON_BOSS then
            -- 判断boss掉落的是否开启
            local areaName = DungeonAreaM.query(info[2], "name");
            nameLabel:setString(areaName..wayName);
            if DungeonAreaM.isParentAreaUnlocked(ME.user, info[2]) then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(true);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 注册链接到boss关卡
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    if UIMgr:getCurrentScene():isOpen("UIEnterDungeon") then
                        UIMgr:getCurrentScene():removeForm("UIEnterDungeon");
                    end
                    local uiItemPath = UIEnterDungeon.create(info[2]);
                    UIMgr.getCurrentScene():addForm(uiItemPath);
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif wayType == WORKSHOP_PRODUCTION then
            -- 炼金工坊出售
            nameLabel:setString(wayName);
            local ress = DungeonAreaM.getOpenResource();
            local unlockIds = DungeonAreaM.getUnlockResource();
            -- 炼金材料开启判断
            if table.indexOf(ress, self.classId - 100) ~= -1 or
               table.indexOf(unlockIds, self.classId - 100) ~= -1 then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            -- 炼金项冈布奥碎片判断
            elseif (self.classId == 2022 and AlchemyWorkshopM.isItemUnlock(13)) or
                   (self.classId == 2026 and AlchemyWorkshopM.isItemUnlock(14)) or
                   (self.classId == 2080 and AlchemyWorkshopM.isItemUnlock(12)) or
                   (self.classId == 2081 and AlchemyWorkshopM.isItemUnlock(11)) then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
                nameLabel:setString(wayName .. getLocStr("not_be_active2"));
            end

            -- 注册链接到炼金工坊
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    local ress = DungeonAreaM.getOpenResource();
                    local unlockIds = DungeonAreaM.getUnlockResource();
                    if table.indexOf(ress, self.classId - 100) ~= -1 then
                        local uiForm = UIWorkshopConfig.create();
                        UIMgr.getCurrentScene():addForm(uiForm);

                    elseif table.indexOf(unlockIds, self.classId - 100) ~= -1 then
                        local uiForm = UIWorkshopStrengthen.create();
                        UIMgr:getCurrentScene():addForm(uiForm);
                        uiForm:selectTab(2);

                        uiForm:initGrids();
                        uiForm:redraw();
                    elseif (self.classId == 2022 and AlchemyWorkshopM.isItemUnlock(13)) or
                           (self.classId == 2026 and AlchemyWorkshopM.isItemUnlock(14)) or
                           (self.classId == 2080 and AlchemyWorkshopM.isItemUnlock(12)) or
                           (self.classId == 2081 and AlchemyWorkshopM.isItemUnlock(11)) then
                        local uiForm = UIWorkshopStrengthen.create();
                        UIMgr:getCurrentScene():addForm(uiForm);
                        uiForm:selectTab(3);

                        uiForm:initGrids();
                        uiForm:redraw();
                    end
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif wayType == BLACKMARKET_SELL then
            -- 旅行商人出售
            nameLabel:setString(wayName);
            local ret  = self:getBlackMacketGoodsId();
            local goodsIndex = self:getIndexs(ret, self.classId);

            -- 是否出售完的标示
            local flag = false;

            if #goodsIndex > 0 then
                for _, value in pairs(goodsIndex) do
                    if "sold_out" ~= BlackMarketM.canBuyGoods(value -1) then
                        flag = true;
                    end
                end
            end

            if flag then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 注册链接旅行商人界面
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    local ret  = self:getBlackMacketGoodsId();
                    if table.indexOf(ret, self.classId) ~= -1 then
                        local uiForm = UIBlackMarket.create();
                        UIMgr.getCurrentScene():addForm(uiForm);
                    end
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif wayType == MARKET_SELL or wayType == MARKET_SELL2 then
            -- 商城出售
            nameLabel:setString(wayName);
            local ids = self:getScrapIds();
            if table.indexOf(ids, self.classId) ~= -1 then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 注册链接到商城界面
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    local ids = self:getScrapIds();
                    if table.indexOf(ids, self.classId) ~= -1 then
                        UIMgr.getCurrentScene():removeFormByName("UIScrapMarket");
                        local uiForm = UIScrapMarket.create("MarketShop");
                        UIMgr.getCurrentScene():addForm(uiForm);
                    end
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif wayType == MARKET_BUY then
            -- 黑市购买
            goImg:setVisible(false);
            touchImg:setVisible(true);
            buyImg:setVisible(true);
            nameLabel:setString(wayName);

            -- 注册打开黑市购买
            local function onBuyClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    UIMgr.getCurrentScene():removeFormByName("UIBuyFood");
                    local uiForm = UIBuyFood.create(self.classId);
                    UIMgr.getCurrentScene():addForm(uiForm);
                    AudioM.playFx("ui_open1");
                end
            end
            buyImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);

        elseif wayType == DUNGEON_SECRET then
            -- 地图隐藏事件
            local areaName = DungeonAreaM.query(info[2], "name");
            nameLabel:setString(spliceLocStr(areaName, wayName));

            if DungeonAreaM.isParentAreaUnlocked(ME.user, info[2]) then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
                nameLabel:setString(spliceLocStr(areaName, wayName, getLocStr("not_open")));
            end

            -- 注册链接到关卡
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    AudioM.playFx("button_click");
                    if UIMgr:getCurrentScene():isOpen("UISubAreaList") then
                        UIMgr:getCurrentScene():removeForm("UISubAreaList");
                    end

                    local uiItemPath = UISubAreaList.create(info[2]);
                    UIMgr.getCurrentScene():addForm(uiItemPath);
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif wayType == SCRAP_SHOP then
            -- 佣兵营地兑换
            nameLabel:setString(wayName);
            local ret  = self:getScrapGoodsId();
            local goodsIndex = self:getIndexs(ret, self.classId);

            -- 是否出售完的标示
            local flag = false;

            if #goodsIndex > 0 then
                for _, value in pairs(goodsIndex) do
                    if "sold_out" ~=PetShopM.canChangeScraps(value) then
                        flag = true;
                    end
                end
            end

            if flag then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 注册链接佣兵营地兑换界面
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    local ret  = self:getScrapGoodsId();
                    if table.indexOf(ret, self.classId) ~= -1 then
                        UIMgr.getCurrentScene():removeFormByName("UIScrapShop");
                        local uiForm = UIScrapShop.create();
                        UIMgr.getCurrentScene():addForm(uiForm);
                    end
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif SIGN_IN_BONUS == wayType then
            -- 签到界面
            goImg:setVisible(true);
            touchImg:setVisible(true);
            buyImg:setVisible(false);
            nameLabel:setString(wayName);
            require "game/ui/form/sign_in/UISignIn";

            -- 打开签到界面
            local function onBuyClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    UIMgr.getCurrentScene():removeFormByName("UISignIn");
                    local uiForm = UISignIn.create();
                    UIMgr.getCurrentScene():addForm(uiForm);
                    AudioM.playFx("ui_open1");
                end
            end
            buyImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);
        elseif BUY_LIFE_EVENT == wayType then
            -- 购买活力触发事件
            goImg:setVisible(false);
            touchImg:setVisible(true);
            buyImg:setVisible(true);
            nameLabel:setString(wayName);

            -- 购买活力触发事件
            local function onBuyClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                -- 如果是体力恢复的商品，若体力已满，直接弹提示
                    if true ~= FormulaM.invoke("CAN_BUY_GOODS_LIFE", ME.user, goodsId) then
                        alert(getLocStr("cannot_buy_life"));
                        return;
                    end

                    local function callback()
                        UIMgr.getCurrentScene():removeFormByName("UIBuyGoods");
                        local uiBuyGoods = UIBuyGoods.create(LIFE_GOODS_ID, 1);
                        UIMgr.getCurrentScene():addForm(uiBuyGoods);
                        AudioM.playFx("ui_open1");
                    end
                    checkIfGuestNeedBind(callback);
                end
            end
            buyImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);
        elseif GAME_ACHIV == wayType then
            -- 成就奖励页面
            goImg:setVisible(true);
            touchImg:setVisible(true);
            buyImg:setVisible(false);
            nameLabel:setString(wayName);

            -- 成就奖励页面
            local function onBuyClick(sender, eventType)
                require "game/ui/form/achievement/UIGameAchiv";
                if eventType == ccui.TouchEventType.ended then
                    UIMgr.getCurrentScene():removeFormByName("UIGameAchiv");
                    local uiForm = UIGameAchiv.create();
                    UIMgr.getCurrentScene():addForm(uiForm);
                    AudioM.playFx("ui_open1");
                end
            end
            goImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);
        elseif BLACKMARKET_EVENT == wayType then
            -- 旅行商人事件触发
            goImg:setVisible(true);
            touchImg:setVisible(true);
            buyImg:setVisible(false);
            if not BlackMarketM.isExist() then
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end
            nameLabel:setString(wayName);

            -- 如果存在旅行商人，旅行商人可打开
            local function onBuyClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    if BlackMarketM.isExist() then
                        UIMgr.getCurrentScene():removeFormByName("UIBlackMarket");
                        local uiForm = UIBlackMarket.create();
                        UIMgr.getCurrentScene():addForm(uiForm);
                        AudioM.playFx("ui_open1");
                    end
                end
            end

            goImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);
        elseif ARTIFACT_EVENT == wayType then
            -- 神器工坊事件
            goImg:setVisible(true);
            touchImg:setVisible(true);
            buyImg:setVisible(false);
            nameLabel:setString(wayName);

            local level = AlchemyWorkshopM.getStrengthenLevel(ARTIFACT_WORKSHOP_ID);

            if not(level > 0) then
                goImg:setVisible(false);
                touchImg:setVisible(false);
                buyImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 直接打开神器工坊
            local function onBuyClick(sender, eventType)
                require "game/ui/form/artifact/UIArtifactSelect"
                if eventType == ccui.TouchEventType.ended then
                    local level = AlchemyWorkshopM.getStrengthenLevel(ARTIFACT_WORKSHOP_ID);

                    if not(level > 0) then
                        return;
                    end

                    UIMgr.getCurrentScene():removeFormByName("UIArtifactSelect");
                    local uiForm = UIArtifactSelect.create();
                    UIMgr.getCurrentScene():addForm(uiForm);
                    AudioM.playFx("ui_open1");
                end
            end
            goImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);
        elseif WORKSHOP_SUMMON == wayType then
            -- 炼金工坊出售

            nameLabel:setString(wayName);
            -- 炼金项冈布奥碎片判断
            if (self.classId == 2022 and AlchemyWorkshopM.isItemUnlock(13)) or
                   (self.classId == 2026 and AlchemyWorkshopM.isItemUnlock(14)) or
                   (self.classId == 2080 and AlchemyWorkshopM.isItemUnlock(12)) or
                   (self.classId == 2081 and AlchemyWorkshopM.isItemUnlock(11)) then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
                nameLabel:setString(wayName .. getLocStr("not_be_active2"));
            end

            -- 注册链接到炼金工坊
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    if (self.classId == 2022 and AlchemyWorkshopM.isItemUnlock(13)) or
                           (self.classId == 2026 and AlchemyWorkshopM.isItemUnlock(14)) or
                           (self.classId == 2080 and AlchemyWorkshopM.isItemUnlock(12)) or
                           (self.classId == 2081 and AlchemyWorkshopM.isItemUnlock(11)) then
                        local uiForm = UIWorkshopStrengthen.create();
                        UIMgr:getCurrentScene():addForm(uiForm);
                        uiForm:selectTab(3);

                        uiForm:initGrids();
                        uiForm:redraw();
                    end
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);

        elseif BUY_GOLD_POT_EVENT == wayType then
            -- 购买黄金罐事件
            goImg:setVisible(false);
            touchImg:setVisible(true);
            buyImg:setVisible(true);
            nameLabel:setString(wayName);

            if not FeatureItemM.isFeatureOpen("scaffold") then
                goImg:setVisible(false);
                touchImg:setVisible(false);
                buyImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 打开黄金罐的购买
            local function onBuyClick(sender, eventType)
                require "game/ui/form/shop/UIBuyPuzzles"
                if eventType == ccui.TouchEventType.ended then
                    UIMgr.getCurrentScene():removeFormByName("UIBuyPuzzles");
                    local goodsId = FormulaM.invoke("CALC_PUZZLE_BOX_COST_CLASS_ID", "gem");
                    local uiForm = UIBuyPuzzles.create(goodsId, "gem");
                    UIMgr.getCurrentScene():addForm(uiForm);
                    AudioM.playFx("ui_open1");
                end
            end
            buyImg:addTouchEventListener(onBuyClick);
            touchImg:addTouchEventListener(onBuyClick);
        elseif GOLD_POT == wayType or GOLD_POT2 == wayType then
            -- 黄金罐的抽取
            goImg:setVisible(true);
            touchImg:setVisible(true);
            buyImg:setVisible(false);
            nameLabel:setString(wayName);

            if not FeatureItemM.isFeatureOpen("scaffold") then
                goImg:setVisible(false);
                touchImg:setVisible(false);
                buyImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
                nameLabel:setString(spliceLocStr(wayName, getLocStr("not_open")));
            end

            -- 打开黄金罐的抽取页面
            local function onBuyClick(sender, eventType)
                require "game/ui/form/shop/UITankUse"
                if eventType == ccui.TouchEventType.ended then
                    UIMgr.getCurrentScene():removeFormByName("UITankUse");
                    local petId = PetM.getPetidByPieceid(self.classId);
                    local campId = CampM.getPetCamp(petId);
                    local uiForm = UITankUse.create("gem", {["camp_id"] = campId});
                    UIMgr.getCurrentScene():addForm(uiForm);
                    AudioM.playFx("ui_open1");
                end
            end
            touchImg:addTouchEventListener(onBuyClick);
            goImg:addTouchEventListener(onBuyClick);

            if type(info) == "table" then
                -- 活动相关，正在活动中的不显示
                if not ActivityM.hasActivityOpenOnce(info[2]) then
                    grid:setVisible(false);
                end
            end
        elseif wayType == SKY_SHOP then
            -- 天空集市
            nameLabel:setString(wayName);
            local ret  = self:getSkyShopGoodsId();
            local goodsIndex = self:getIndexs(ret, self.classId);

            -- 是否出售完的标示
            local flag = false;

            if #goodsIndex > 0 then
                for _, value in pairs(goodsIndex) do
                    if "sold_out" ~= ShopM.canBuyGoods("sky_shop", value - 1) then
                        flag = true;
                    end
                end
            end

            if flag then
                goImg:setVisible(true);
                touchImg:setVisible(true);
            else
                goImg:setVisible(false);
                touchImg:setVisible(false);
                TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, false, kerning);
            end

            -- 注册链接天空集市界面
            local function onGoClick(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    local ret  = self:getSkyShopGoodsId();
                    if table.indexOf(ret, self.classId) ~= -1 then
                        UIMgr.getCurrentScene():removeFormByName("UISkyShop");
                        local uiForm = UISkyShop.create();
                        UIMgr.getCurrentScene():addForm(uiForm);
                    end
                end
            end
            goImg:addTouchEventListener(onGoClick);
            touchImg:addTouchEventListener(onGoClick);
        end
    end
end

-- 获取黑市商品的id
function UIGainItemPath:getBlackMacketGoodsId()
    -- 获取本店的所有商品
    local goodsList = BlackMarketM.getAllGoods();
    if goodsList == nil then
        return nil;
    end

    local ret = {};
    local goodsIndex = 1;  -- 商品的序号
    for _, info in pairs(goodsList) do
        local goodsId    = info.goods_id;
        local itemInfo;

        if info.goods_type == "honor_scrap" then
            -- 是冈布奥碎片
            itemInfo = info["goods"];
        else
            itemInfo = BlackMarketM.queryGoodsInfo(goodsId);
        end
        local classId    = itemInfo[2];
        if itemInfo then
            table.insert(ret, classId);
        end

        goodsIndex = goodsIndex + 1;
    end

    return ret;
end

-- 获取碎片的id
function UIGainItemPath:getScrapIds()
     local ids = {};
     local finalList = MarketM:getMarketSellScrapList();
     if #finalList == 0 then
        return ids;
     end

     for i = 1, #finalList do
         local info = finalList[i].bonus;
         table.insert(ids, info[1][2]);
     end

     return ids;
end

-- 查找所有的相同的
function UIGainItemPath:getIndexs(t, id)
    local indexs = {};
    for i = 1, #t do
        if t[i] == id then
            table.insert(indexs, i);
        end
    end

    return indexs;
end

-- 获取佣兵营地可兑换商品的id
function UIGainItemPath:getScrapGoodsId()
    -- 获取本店的所有商品
    local goodsList = PetShopM.getAllScrapGoods();
    if goodsList == nil then
        return nil;
    end

    local ret = {};
    for _, info in pairs(goodsList) do
        local classId = info["rare_pet_chips"];
        if classId then
            table.insert(ret, classId);
        end
    end

    return ret;
end

-- 获取天空集市商品的id
function UIGainItemPath:getSkyShopGoodsId()
    -- 获取本店的所有商品
    local goodsList = ShopM.getAllGoods("sky_shop");

    if goodsList == nil then
        return nil;
    end

    local ret = {};
    for _, info in pairs(goodsList) do
        local goodsId = info.goods_id;
        local goodsInfo;

        if info.goods_type == "honor_scrap" then
            goodsInfo = info.goods;
        else
            goodsInfo = ShopM.queryGoodsInfo(goodsId);
        end
        local classId = goodsInfo[2];
        if classId then
            table.insert(ret, classId);
        end
    end

    return ret;
end